package com.example.bishe.controller.zy;

import com.alibaba.fastjson.JSON;
import com.example.bishe.entity.Bzks_zy;
import com.example.bishe.entity.Jxbm;
import com.example.bishe.service.Bzks_zyService;
import com.example.bishe.service.JxbmService;
import com.example.bishe.service.impl.Bzks_zyServiceImpl;
import com.example.bishe.service.impl.JxbmServiceImpl;
import com.example.bishe.toolclass.CUD;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author yws
 * @Date 2022/4/4 13:56
 */
@RestController
@RequestMapping("/zy")
public class ZyController {
    @Resource
    CUD CUD;
    @Autowired
    Bzks_zyService zy = new Bzks_zyServiceImpl();
    @Autowired
    JxbmService jxbmService = new JxbmServiceImpl();
    @PostMapping("/addzy")
    public String addzy(@RequestBody Bzks_zy bzks_zy) {
        int errcode = 0;//错误代码
        String errmsg =" ";//错误描述
        int id;//返回创建成功的id
        HashMap value = null;
        Bzks_zy bzksZy = new Bzks_zy();bzksZy.setDm(bzks_zy.getDm());
        Bzks_zy[] bzks_zies = zy.select_zy(bzksZy);
        if (bzks_zy.getDm()==null)
            errmsg+="请求中没有添加专业代码，";
        if (bzks_zy.getMc()==null)
            errmsg+="请求中没有添加专业名称，";
        if(bzks_zy.getDwdm()==null)
            errmsg+="请求中没有添加专业上层部门代码，";
        if(bzks_zy.getXz()==0)
            errmsg+="请求中没有添加专业的学制，";
        if(errmsg.length()>1){
            errcode = 1401;
            errmsg = errmsg.substring(1, errmsg.length() - 1) + "。";
        }else if(bzks_zies.length==0){
            Jxbm jxbm = new Jxbm();jxbm.setDm(bzks_zy.getDwdm());
            Jxbm[] jxbms = jxbmService.selectJxbm(jxbm);
            if(jxbms.length!=0){
                jxbm = jxbms[0];
                value = CUD.add(bzks_zy.getMc(),
                        jxbm.getWx_id()+"",
                        bzks_zy.getWx_id()!=0?bzks_zy.getWx_id()+"":"");
                if (value.get("errcode").equals("0")) {
                    errcode = 0;
                    //可以插入数据库操作
                    bzks_zy.setWx_id(Integer.parseInt((String) value.get("id")));
                    bzks_zy.setWx_parentid(jxbm.getWx_id());
                    zy.insert_zy(bzks_zy);
                    errmsg += "无错误。";
                    errmsg = errmsg.substring(1, errmsg.length());
                }else {
                    errcode = 1401;
                    errmsg+= CUD.addpan(value);
                }
            }else {
                errcode = 1401;
                errmsg += "你选择的教学部门不存在";
            }
        }else {
            errcode = 1401;
            errmsg += "你所创建的专业代码已经存在。";
            errmsg = errmsg.substring(1, errmsg.length());
        }
        Map<String, String> map = new HashMap();
        map.put("errcode", String.valueOf(errcode));
        map.put("errmsg", errmsg);
        if (bzks_zy.getMc() == null) {
            map.put("mingcheng", "null");
        } else {
            map.put("mingcheng", bzks_zy.getMc());
        }
        String s = JSON.toJSONString(map);
        return s;
    }
    @PostMapping("/deletezy")
    public String deletezy(@RequestBody Bzks_zy bzks_zy) {
        int errcode = 0;//错误代码
        String errmsg = "";//错误描述
        if (bzks_zy.getDm() == null) {
            errcode = 1402;
            errmsg = "请传入你要删除的专业代码";
        }else {
            Bzks_zy bzksZy = new Bzks_zy();bzksZy.setDm(bzks_zy.getDm());
            Bzks_zy[] bzks_zies = zy.select_zy(bzksZy);
            if(bzks_zies.length!=0){
                bzksZy = bzks_zies[0];
                HashMap<String, String> value = CUD.delete(bzksZy.getWx_id()+"");
                if (value.get("errcode").equals("0")) {
                    zy.delete_zy(bzksZy);
                    errmsg = "已删除对应的专业";
                } else {
                    errmsg+= CUD.deletepan(value);
                    errcode=1402;
                }
            }else {
                errcode = 1402;
                errmsg = "没有找到对应的专业信息";
            }

        }
        Map map = new HashMap();
        map.put("errcode", String.valueOf(errcode));
        map.put("errmsg", errmsg);
        String s = JSON.toJSONString(map);
        return s;
    }

    @PostMapping("/updatezy")
    public String updatezy(@RequestBody Bzks_zy bzks_zy) {
        int errcode = 0;//错误代码
        String errmsg = "";//错误描述
        if(bzks_zy.getWx_id()!=0||bzks_zy.getWx_parentid()!=0){
            errcode = 1403;
            errmsg = "不能修改专业的微信id，父部门id请修改部门的隶属教学部门代码";
        }
        if (bzks_zy.getDm() == null) {
            errcode = 1403;
            errmsg = "请传入你要修改的专业代码";
        }else {
            Bzks_zy bzksZy = new Bzks_zy();bzksZy.setDm(bzks_zy.getDm());
            Bzks_zy[] bzks_zies = zy.select_zy(bzksZy);
            if(bzks_zies.length!=0){
                bzksZy = bzks_zies[0];/*c查找到的原始专业信息*/
                if(bzks_zy.getDwdm()!=null){
                    Jxbm jxbm = new Jxbm();jxbm.setDm(bzks_zy.getDwdm());
                    Jxbm[] jxbms = jxbmService.selectJxbm(jxbm);
                    if(jxbms.length!=0){
                        jxbm = jxbms[0];
                        HashMap<String, String> value = CUD.update(bzksZy.getWx_id()+"",
                                bzks_zy.getMc(),
                                jxbm.getWx_id()+"");
                        System.out.println(value.get("errcode"));System.out.println(value.get("errmsg"));
                        if(value.get("errcode").equals("0")){
                            errmsg+="已修改对应信息";
                            bzks_zy.setWx_id(bzksZy.getWx_id());
                            bzks_zy.setWx_parentid(jxbm.getWx_id());
                            zy.update_zy(bzks_zy);
                        }else {
                            errcode=1403;
                            errmsg+= CUD.updatepan(value);
                        }
                    }else {
                        errcode = 1403;
                        errmsg = "没有找到你想修改的教学部门信息";
                    }
                }else {
                    HashMap<String, String> value = CUD.update(bzksZy.getWx_id()+"",
                            bzks_zy.getMc(),
                            bzksZy.getWx_parentid()+"");
                    System.out.println(value.get("errcode"));System.out.println(value.get("errmsg"));
                    if(value.get("errcode").equals("0")){
                        errmsg+="已修改对应信息";
                        bzks_zy.setWx_id(bzksZy.getWx_id());
                        bzks_zy.setWx_parentid(bzksZy.getWx_parentid());
                        zy.update_zy(bzks_zy);
                    }else {
                        errcode=1403;
                        errmsg+= CUD.updatepan(value);
                    }
                }
            }else {
                errcode = 1403;
                errmsg = "没有找到对应的专业信息";
            }
        }
        Map map = new HashMap();
        map.put("errcode", String.valueOf(errcode));
        map.put("errmsg", errmsg);
        String s = JSON.toJSONString(map);
        return s;
    }

    @PostMapping("/selectzy")
    public String selectzy(@RequestBody Bzks_zy bzks_zy) {
        Bzks_zy[] bzks_zyes = zy.select_zy(bzks_zy);
        List<Bzks_zy> list = new ArrayList<>();
        for (int i = 0; i < bzks_zyes.length; i++) {
            list.add(bzks_zyes[i]);
        }
        Map map = new HashMap();
        map.put("Bzks_zy",list);
        String s = JSON.toJSONString(map);
        return s;
    }
}
